/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectRiskController
 * 项目风险
 *
 * 修改纪录
 * 2021-08-19 版本：1.0 xinfushuo 创建。
 * @version 版本：1.0
 * @author 作者：xinfushuo
 * 创建日期：2021-08-19
 */

package com.topscomm.mpm.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.topscomm.cbo.pojo.CboItemsEntity;
import com.topscomm.cbo.pojo.CboSysParamEntity;
import com.topscomm.cbo.service.impl.CboItemsService;
import com.topscomm.cbo.service.impl.CboSysParamService;
import com.topscomm.mpm.pojo.MpmProjectDemandEntity;
import com.topscomm.mpm.pojo.MpmProjectRiskEntity;
import com.topscomm.pub.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.mpm.controller.auto.MpmProjectRiskControllerAuto;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst.TapOperationTypeEnum;

import javax.swing.*;

/**
 * @author: xinfushuo
 * @date: 2021-08-19
 * @description:项目风险Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectRisk")
public class MpmProjectRiskController extends MpmProjectRiskControllerAuto implements IMpmProjectRiskController {
	@Autowired
	CboSysParamService cboSysParamService;
	/**
	 * @author: xinfushuo
	 * @date: 2021-08-19
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify: 21.8.24 添加项目id条件
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled=1 ");
		if (paraMap.containsKey("qry_user")) {
			String value = paraMap.get("qry_user").toString();
			if (!StringUtil.isEmpty(value))
				if(value.matches("^[0-9]*$") || value.matches(".*\\d+.*")){
					sql.appendSingle(" and " + MpmProjectRiskEntity.FieldUsercode + " like '{0}%'", value);
				}else{
					sql.appendSingle(" and " + MpmProjectRiskEntity.FieldUsername + " like '%{0}%'", value);
				}
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_docno")) {
			String value = paraMap.get("qry_docno").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docno like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_begindocdate")) {
			String value = paraMap.get("qry_begindocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate >= {0} ", SQLAdapter.toDateLong(value + " 00:00:00"));
		}
		if (paraMap.containsKey("qry_enddocdate")) {
			String value = paraMap.get("qry_enddocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate <= {0}", SQLAdapter.toDateLong(value + " 23:59:59"));
		}
		if (paraMap.containsKey("qry_deptname")) {
			String value = paraMap.get("qry_deptname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (deptid in ( SELECT id from CboDept where fullname like '%{0}%'))", value);
		}
		if (paraMap.containsKey("qry_username")) {
			String value = paraMap.get("qry_username").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (username like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_deptcode")) {
			String value = paraMap.get("qry_deptcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcode like '{0}%'", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}

		//添加 项目id 查询参数 只返回当前项目下所有数据
		if (paraMap.containsKey("qry_projectid")) {
			String value = paraMap.get("qry_projectid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and projectid={0}", value);
		}
		if (paraMap.containsKey("qry_where")) {
			String value = paraMap.get("qry_where").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}", value);
		}
		return sql.toString();
	}

	/**
	 * @author: xinfushuo
	 * @date: 2021-08-19
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 *
	 * @description: 提交审批前
	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author: xinfushuo
	 * @date: 2021-08-19
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}{1}于{2}提交项目风险申请,单据编号{3}! ", dataMap.get("dispdeptcode"), dataMap.get("username"), DateUtil.dateToString(new Date(), "yyyy-MM-dd"), dataMap.get("docno"));
		dataMap.put("maintitle", mailtitle.toString());
	}
	/**
	  * @description:获取风险对应的codelist
	  * @author: xinfushuo
	  * @param:
	  * @throws:
	  * @return: 风险对应 所有编码字段对应的dataCodeList
	  * @date: 2021/8/19
	  * @modify:
	  */
	@Override
	@GetMapping(value = "/dataCodeList")
	public ResponseResult<Object> dataCodeList(@RequestParam(required = false) Map<String, Object> paraMap){
		Map<String, Object> codeMap = new HashMap<String, Object>();
		//风险影响等级
		List<Map<String, Object>> riskInfluenceLevelList = datacodeCache.getCodeListMap("MpmProjectRiskInfluenceLevel", "", "sortcode");
		codeMap.put("riskInfluenceLevelList", riskInfluenceLevelList);
		//风险可能性
		List<Map<String, Object>> riskProbabilityList = datacodeCache.getCodeListMap("MpmProjectRiskProbability", "", "sortcode");
		codeMap.put("riskProbabilityList", riskProbabilityList);
		//风险性质
		List<Map<String, Object>> riskPropertyList = datacodeCache.getCodeListMap("MpmProjectRiskProperty", "", "sortcode");
		codeMap.put("riskPropertyList", riskPropertyList);
		//风险等级
		List<Map<String, Object>> riskLevelList = datacodeCache.getCodeListMap("MpmProjectRiskLevel", "", "sortcode");
		codeMap.put("riskLevelList", riskLevelList);
		//风险趋势
		List<Map<String, Object>> riskTrendList = datacodeCache.getCodeListMap("MpmProjectRiskTrend", "", "sortcode");
		codeMap.put("riskTrendList", riskTrendList);
		//风险状态
		List<Map<String, Object>> riskStatusList = datacodeCache.getCodeListMap("MpmProjectRiskStatus", "", "sortcode");
		codeMap.put("riskStatusList", riskStatusList);
		//调用父类auto 方法 并 合并两个codemap 当重新生成代码时 需要重新配置auto中的 approvalflowlist
		Map<String,Object> autoMap = (Map<String,Object>)super.dataCodeList(paraMap).getResult();
		codeMap.putAll(autoMap);

		return ResponseResult.ok(codeMap);

	}
	/**
	  * @description: 获取风险等级匹配矩阵，格式为json二维数组
	  * @author: xinfushuo
	  * @param: 无
	  * @throws:
	  * @return: responseresult 二维数组
	  * @date: 2021/8/25
	  * @modify:
	  */
	@GetMapping(value = "/riskLevelMatrix")
	public ResponseResult<Object> getRiskLevelMatrix(@RequestParam(required = false)Map<String,Object> paraMap){
		Map<String, Object> result = new HashMap<String, Object>();

		CboSysParamEntity riskMatrix = (CboSysParamEntity) cboSysParamService.queryByWhere(" code = 'MpmProjectRiskMatrix' " ).get(0);
		//获取系统参数 格式为json二维数组 可在系统参数管理中编辑
		result.put("riskLevelMatrix",riskMatrix.getParamvalue());
		return ResponseResult.ok(result);

	}
}

